//   @Name    : merchant_driver
//   @author  : 海参
//   @date    : 2022/4/08 19:01 AM
//   @version : 1.0
//   @desc    :自定义可以设置按钮大小的图标按钮；系统按钮有默认最小宽度/高度约束值Size(48pt,48pt),也有默认padding，
//   在遇到只需要高度小一点的图标按钮时就不适合用，比如标签文本前面打勾的图标按钮。
//   2024-09-27 重构

import 'package:flutter/material.dart';

/*例如：
  Widget rememberContainerWidget() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        CaiNoConstraintsIconButton(
            buttonSize: Size(30.w, 30.w),
            icon: CaiImage.assetImage(
                name: _isRememberAccount ? 'login_select' : 'login_unSelect',
                width: 15.w,
                height: 15.w),
            style: IconButton.styleFrom(),
            onPressed: () {
              setState(() {
                _isRememberAccount = !_isRememberAccount;
              });
            }),
        Text.rich(TextSpan(
          text: '记住账户',
          style: TextStyle(color: const Color(0xff333333), fontSize: 12.sp),
          recognizer: _tapAccountRecognizer,
        )),
        const Expanded(child: SizedBox()),
      ],
    );
  }
 */

class CaiNoConstraintsIconButton extends StatelessWidget {
  //m3之后改用ButtonStyle设置
  final double iconSize;
  //m3之后改用ButtonStyle设置
  final EdgeInsetsGeometry padding;
  //m3之后改用ButtonStyle设置。只对icon是[Icon], [ImageIcon]时有效，对Image.asset无效。[一般不用]
  final Color? color;

  final Size? buttonSize;

  final VoidCallback? onPressed;

  ///minimumSize:Size(48,48),iconSize,padding,color都用这个属性设置。
  final ButtonStyle? style;
  //iconButton选择状态。此属性仅在[ThemeData.useMaterial3]为真时使用。【不用】
  final bool? isSelected;
  //当属性[isSelected]是true的时候，显示的icon图标。
  //此属性仅在[ThemeData.useMaterial3]为真时使用。【不用】
  final Widget? selectedIcon;
  //使用Image.asset设置icon属性：由assetImage自己控制size大小;[Icon], [ImageIcon]由iconSize属性控制；
  //如果要让icon的实际大小受控制，则iconSize和icon的大小要一样大;
  final Widget icon;

  const CaiNoConstraintsIconButton({
    super.key,
    this.iconSize = 24.0,
    this.padding = const EdgeInsets.all(0),
    this.color,
    required this.onPressed,
    this.style,
    this.isSelected,
    this.selectedIcon,
    required this.icon,
    this.buttonSize,
  });

  @override
  Widget build(BuildContext context) {
    IconButton iconButton = IconButton(
      iconSize: iconSize,
      padding: padding,
      color: color,
      onPressed: onPressed,
      style: style,
      isSelected: isSelected,
      selectedIcon: selectedIcon,
      icon: icon,
    );
    if (buttonSize != null) {
      return SizedBox(
        width: buttonSize!.width,
        height: buttonSize!.height,
        child: iconButton,
      );
    }
    return iconButton;
  }
}
